package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.constants.MessageConstant;
import com.huike.dto.BusinessPoolPageDto;
import com.huike.entity.Clue;
import com.huike.entity.Customer;
import com.huike.mapper.CustomerMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.BusinessPoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BusinessPoolServiceImpl implements BusinessPoolService {

    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public Result pagePoolList(BusinessPoolPageDto dto) {
        if (dto == null) {
            return Result.error(MessageConstant.DATA_NOT_EXIST);
        }
        if(dto.getPage()==null||dto.getPageSize()==null){
            dto.setPage(1);
            dto.setPageSize(5);
        }
        IPage<Customer> pageResult = new Page<>(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<Customer> lqw = new LambdaQueryWrapper<>();
        lqw.like(dto.getName() != null, Customer::getName, dto.getName());
        lqw.like(dto.getPhone() != null, Customer::getPhone, dto.getPhone());
        lqw.like(dto.getCustomerId() != null, Customer::getCustomerId, dto.getCustomerId());
        lqw.like(dto.getSubject()!=null,Customer::getSubject,dto.getSubject());

        //是否在线索池 0否 1是
        lqw.eq(Customer::getIsHighSea, 1);
        Date serachTime = dto.getCreateTime();
        if (serachTime!=null){
            Instant instant = serachTime.toInstant();
            // 将 Instant 转换为 LocalDateTime
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            LocalDateTime minDateTime = localDateTime.with(LocalTime.MIN);
            LocalDateTime maxDateTime = localDateTime.with(LocalTime.MAX);
            lqw.le(Customer::getCreateTime, maxDateTime)
                    .ge(Customer::getCreateTime, minDateTime);
        }

        customerMapper.selectPage(pageResult, lqw);
        PageResult result = new PageResult(pageResult.getTotal(), pageResult.getRecords());


        return Result.success(result);
    }

    @Override
    public Result gain(Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        List<Customer> clues = customerMapper.selectBatchIds(list);
        List<Customer> collect = clues.stream().map(customer -> {
            //不加入公海池
            customer.setIsHighSea((short)0);
            return customer;
        }).collect(Collectors.toList());
        for (Customer customer : collect) {
            customerMapper.updateById(customer);
        }
        return Result.success();
    }

    @Override
    public Result assign(List<Long> list, String userName) {
        List<Customer> customers = customerMapper.selectBatchIds(list);
        List<Customer> collect = customers.stream().map(customer -> {
            customer.setBelonger(userName);
            return customer;
        }).collect(Collectors.toList());
        //TODO:优化批量添加,userId和belonger关系
        for (Customer customer : collect) {
            customerMapper.updateById(customer);
        }

        return Result.success();
    }
}
